<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="场馆名称" prop="venueName">
        <el-input
          v-model="queryParams.venueName"
          placeholder="请输入场馆名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="场馆照片" prop="venuePhoto" v-if="false">
        <el-input
          v-model="queryParams.venuePhoto"
          placeholder="请输入场馆照片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="场馆容量" prop="venueCapacity">
        <el-input
          v-model="queryParams.venueCapacity"
          placeholder="请输入场馆容量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场馆位置" prop="venueLocation">
        <el-input
          v-model="queryParams.venueLocation"
          placeholder="请输入场馆位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hj:venues:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hj:venues:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hj:venues:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hj:venues:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="venuesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="venueId" v-if="false" />
      <el-table-column label="场馆名称" align="center" prop="venueName" />
      <el-table-column label="场馆照片" align="center" prop="venuePhoto" v-if="false" />

      <el-table-column label="场馆照片" align="center" v-if="false">  
        <template slot-scope="scope">  
          <img :src="scope.row.venuePhoto" alt="场馆照片" style="width: 120px; height: 80px;">  
        </template>  
      </el-table-column>

      <el-table-column label="场馆照片" align="center">  
        <template slot-scope="scope">  
          <img  
            :src="scope.row.venuePhoto"  
            alt="场馆照片"  
            style="width: 120px; height: 80px; cursor: pointer;"  
            @click="handleImageClick(scope.row.venuePhoto)"  
          />  
        </template>  
      </el-table-column>

      <el-table-column label="场馆容量" align="center" prop="venueCapacity" />
      <el-table-column label="场馆位置" align="center" prop="venueLocation" />

      <el-table-column prop="venueStatus" label="场馆状态">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_venue_status" :value="scope.row.venueStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hj:venues:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hj:venues:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

     <el-dialog
  :visible.sync="imageViewerVisible"
  width="50%"
  title="图片预览"
  @close="handleImageClose"
   >
  <img :src="url" alt="场馆照片" style="width: 100%;" />
    </el-dialog>

    <!-- 添加或修改场馆对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="场馆名称" prop="venueName">
          <el-input v-model="form.venueName" placeholder="请输入场馆名称" />
        </el-form-item>

        <el-form-item label="场馆照片" prop="venuePhoto" v-if="false">
          <el-input v-model="form.venuePhoto" placeholder="请输入场馆照片" />
        </el-form-item>

      <el-form-item label="图片" >
        <el-image
          style="width: 150px; height: 150px"
            :src="url"
            :fit="fit" ref="imageId" >
        </el-image>

        
        <el-upload
          ref="uploadFile"
          :limit="1"
          accept=".bmp,.jpg,.jpeg,.png,gif"
          :headers="uploadData.headers"
          :action="uploadData.url"
          :disabled="uploadData.isUploading"
          :on-progress="handleFileUploadDataProgress"
          :on-success="handleFileDataSuccess"
          :auto-upload="false"
         drag
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <span>仅允许导入bmp、jpg、jpeg、gif、png格式文件。</span>
          
        </el-upload>
                                         
        <el-button type="primary" @click="submitFileFormImage">上传图片</el-button>


      </el-form-item>



        <el-form-item label="场馆容量" prop="venueCapacity">
          <el-input v-model="form.venueCapacity" placeholder="请输入场馆容量" />
        </el-form-item>
        <el-form-item label="场馆位置" prop="venueLocation">
          <el-input v-model="form.venueLocation" placeholder="请输入场馆位置" />
        </el-form-item>

        <el-form-item label="场馆状态" v-if="status">
              <el-radio-group v-model="form.venueStatus">
                <el-radio
                  v-for="dict in dict.type.sys_venue_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
        </el-form-item>

        

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVenues, getVenues, delVenues, addVenues, updateVenues, countSchedule } from "@/api/hj/venues";
import { getToken } from "@/utils/auth";

export default {
  name: "Venues",
  dicts: ['sys_venue_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场馆表格数据
      venuesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示状态选项
      status: false,

      // 图片导入参数
      uploadData: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/hj/venues/uploadMinio"
      },
      imageViewerVisible: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        venueName: null,
        venuePhoto: null,
        venueCapacity: null,
        venueLocation: null,
        venueStatus: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        venueName: [
          { required: true, message: "场馆名称不能为空", trigger: "blur" }
        ],
        venueLocation: [
          { required: true, message: "场馆位置不能为空", trigger: "blur" }
        ],
        venueStatus: [
          { required: true, message: "场馆状态不能为空", trigger: "change" }
        ]
      },
      // 初始图片URL 
      url:'',
      // 图片适应方式，这里设置为'contain' 
      fit:'contain',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询场馆列表 */
    getList() {
      this.loading = true;
      listVenues(this.queryParams).then(response => {
        this.venuesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        venueId: null,
        venueName: null,
        venuePhoto: null,
        venueCapacity: null,
        venueLocation: null,
        venueStatus: null
      };
      this.resetForm("form");
      this.url = ''; // 重置图片URL
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.venueId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加场馆";
      this.status = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const venueId = row.venueId || this.ids
      getVenues(venueId).then(response => {
        console.log(response);
        this.form = response.data;
        this.open = true;
        this.title = "修改场馆";

        this.url = this.form.venuePhoto;

        this.status = true;

      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.venueId != null) {
            updateVenues(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVenues(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const venueIds = row.venueId || this.ids;
      this.$modal.confirm('是否确认删除场馆编号为"' + venueIds + '"的数据项？').then(function() {
        return delVenues(venueIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hj/venues/export', {
        ...this.queryParams
      }, `venues_${new Date().getTime()}.xlsx`)
    },

    // 文件上传中处理
    handleFileUploadDataProgress(event, file, fileList) {
      console.log(getToken());
      console.log(event);
      console.log(file+"333");
      console.log(fileList+"333");
      this.uploadData.isUploading = true;
    },
    // 文件上传成功处理
    handleFileDataSuccess(response, file, fileList) {
       console.log(response);
       console.log(response.msg);
       console.log(file+"444");
       console.log(fileList+"555");
      this.uploadData.open = false;
      this.uploadData.isUploading = false;
      this.$refs.uploadFile.clearFiles();

      this.form.venuePhoto = response.msg;

      this.url = this.form.venuePhoto;

      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + "上传图片成功！" + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
    },
    // 提交上传文件
    submitFileFormImage() {
      this.$refs.uploadFile.submit();
    },

    /**  
     * 查看场馆照片  
     * @param {string} photoUrl - 照片URL  
     */  
    viewPhoto(photoUrl) {  
      window.open(photoUrl, '_blank');  
    }, 
     handleImageClick(venuePhoto) {
      this.url = venuePhoto;
      this.imageViewerVisible = true;
    },
    handleImageClose() {
      this.url = '';
      this.imageViewerVisible = false;
    },
  }
};
</script>
